<template>
  <div class="login">
    <!-- 登录框 -->
    <div class="login-circle">
      <!-- 头像 -->
      <div class="img">
        <img src="../assets/logo.png" />
      </div>
      <!-- 表单项 -->
      <el-form ref="loginFormRef" label-width="0px" class="form" :model="loginform" :rules="rules">
        <el-form-item class="uname" prop="username">
          <el-input
            v-model="loginform.username"
            placeholder="请输入用户名"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item class="pwd" prop="password">
          <el-input
            v-model="loginform.password"
            placeholder="请输入密码"
            show-password
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="validata">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginform: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        // 验证用户名
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        // 验证秘法是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 表单重置操作
    reset: function() {
      // console.log(this);
      this.$refs.loginFormRef.resetFields()
    },
    // 点击登录按钮 对表单进行验证
    validata: function() {
      this.$refs.loginFormRef.validate(async vali => {
        // console.log(vali);
        // 判断验证结果 成功则去后台发登录请求
        if (!vali) {
          return this.$message.error('用户名或密码不符合')
        }
        var { data: res } = await this.$http.post('login', this.loginform)
        if (res.meta.status !== 200) {
          this.$message.error('登陆失败')
          return
        }
        this.$message.success('登陆成功')
        console.log(res)

        window.sessionStorage.setItem('token', res.data.token)

        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  background-color: #2b4b6b;
  width: 100%;
  height: 100%;
  .login-circle {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 400px;
    height: 250px;
    background-color: #fff;
    border-radius: 5px;
    .img {
      height: 100px;
      width: 100px;
      border-radius: 50%;
      box-sizing: border-box;
      padding: 5px;
      border: 1px solid #eee;
      background-color: #eee;
      position: absolute;
      top: -50px;
      left: 50%;
      transform: translate(-50%, 0);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #ccc;
      }
    }
    .form {
      position: absolute;
      width: 100%;
      bottom: -10px;
      padding: 10px;
      box-sizing: border-box;
      .uname {
        width: 100%;
      }
      .pwd {
        width: 100%;
      }
      .btn {
        display: flex;
        justify-content: flex-end;
        padding-right: 20px;
      }
    }
  }
}
</style>
